<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <el-input v-model="query.userName" clearable size="small" placeholder="输入姓名搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="listUser" />
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="listUser">搜索</el-button>
      </div>
    </div>
    <div class="crud-opts">
      <el-button
        class="filter-item"
        size="mini"
        type="primary"
        icon="el-icon-plus"
        @click="toAdd"
      >
        新增
      </el-button>
      <el-button
        class="filter-item"
        type="danger"
        icon="el-icon-delete"
        size="mini"
        :loading="loading"
        @click="batchDelete()"
      >
        删除
      </el-button>
    </div>
    <!--表格渲染-->
    <el-table ref="table" v-loading="loading" :data="userList" style="width: 100%;" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="userName" label="姓名" />
      <el-table-column prop="sex" label="性别" />
      <!--   编辑与删除   -->
      <el-table-column
        label="操作"
        width="260px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            class="filter-item"
            size="mini"
            type="success"
            icon="el-icon-edit"
            @click="toEdit(scope.row.id)"
          >
            修改
          </el-button>
          <el-button
            class="filter-item"
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :loading="loading"
            @click="toDelete(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :page-size.sync="page.size"
      :total="page.total"
      :current-page.sync="page.page"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChangeHandler"
      @current-change="pageChangeHandler"
    />
    <!--表单渲染-->
  </div>
</template>

<script>
import { del, getAllUser } from '../../../api/exercise/user.js'

export default {
  name: 'ExerciseUser',
  components: { },
  mixins: [],
  data() {
    return {
      query: { userName: '' },
      page: { size: 10, total: 0, page: 1, totalCount: 0, totalPageCount: 0 },
      multipleSelection: [],
      userList: [],
      loading: false
    }
  },
  created() {
    this.listUser()
  },
  methods: {
    listUser() {
      const pg = this.page.page
      getAllUser(this.query, this.page).then(res => {
        this.userList = res.content
        this.page.total = res.totalElements
        this.page.page = pg
      }, err => {
        this.$message.warning(err)
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    sizeChangeHandler(val) {
      this.page.size = val
      this.page.page = 1
      this.listUser()
    },
    pageChangeHandler(val) {
      this.page.page = val
      this.listUser()
    },
    toAdd() {
      this.$router.push({ 'path': '/exercise2/userAdd' })
    },
    toEdit(id) {
      this.$router.push({ 'path': '/exercise2/userEdit/' + id })
    },
    toDelete(id) {
      console.log(id)
      this.$confirm('您确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del([id]).then(res => {
          this.listUser()
          if (res.status === 200) {
            this.$message.success('删除成功')
          }
        })
      })
    },
    batchDelete() {
      const ids = this.multipleSelection.map(item => {
        return item.id
      })
      console.log(ids)
      this.$confirm('您确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(ids).then(res => {
          this.listUser()
          this.$message.success('删除成功')
        })
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
 ::v-deep .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
